import formJs from "./regexDetail.js"; //新增弹框配置项
import TableLyout from '@/components/commonPublic/tableLyoutDetail.vue'
import TableData from "@/components/public/el-table-pagination";
import publicForm from "@/components/public/up-form.vue"; //弹框form

import {
    getKeyInfoDetailList,//列表
    getKeyInfoDetail,//
    saveKeyDetail, //保存
} from "@/api/BackStage/stationProductionPage/request.js";

export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
    ],
    data() {
        return {
            visible: "",
            title: null,
            isLock: false,
            dialogHeight: null,
            btnShow: true,
            newRow: null,
            loading: false,

            // 列表
            getList: {
                fId: "", // 左侧选中组织机构id
                page: 1, // 当前页码
                pageSize: 50, // 每页显示条数
                userId: "", // 查询待办或已办时传 列表页查询不传
            },
            table_config: formJs.table_config, //列表
            tableData: [],
            RowData: null, //列表详情
            dataFid: null, //列表详情id
            // 列表
            // 详情
            formObj: formJs.formObj, //基础信息
            formData: {
                fId: "", //传fid表示修改 不传表示新增
                parentId: "", //父表id
                monitorCompanyType: "", //工程类型
                monitorCompany: "", //监测单位
                monitorDate: "",
                monitorDates: "", //监测时间
                lastMonitor: "", //距上次监测时长（天）
                dynamicAnalysis: "", //动态分析
                monitorInfo: "", //监测照片资料
                monitorVideo: "",//监测视频资料
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
            },
            fileList: [],
            fileList2: [],
            // 详情

            timeYMD: "",
            timeYmdHms: "",

        };
    },
    components: {
        TableLyout,
        TableData,
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.newRow = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
        this.getData();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        console.log(this.userInfo);
        this.getList.fId = this.newRow.fId;

        this.formObj.map(item => {
            item.disabled = true;
        })

        this.btnShow = true;
        if (this.statusNum == 5) { //编辑
            this.formObj[4].disabled = false;
            this.formObj[5].disabled = false;
            this.formObj[4].uploadSelect = true;
            this.getKeyInfoDetail(this.newRow.fId, 1);
        } else if (this.statusNum == 7) { //查看
            this.yj_searchForm(this.getList);
            this.btnShow = false;
            this.formObj[4].uploadSelect = false;
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        closeDialog() {
            this.$emit("close");
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        selectChange(data, a, b) {
            console.log("selectChange", data, a, b);
        },
        // 点击当前行
        rowChange(val) {
            this.RowData = val;
            this.dataFid = val.fId;
            this.getKeyInfoDetail(val.fId, 2);
        },
        // 获取列表数据
        yj_searchForm(data) {
            var obj = {
                fId: data.fId, //左侧选中组织机构id
                page: data.page, //当前页码
                pageSize: data.pageSize, //每页显示条数
                userId: this.userInfo.userId, //查询待办或已办时传 列表页查询不传
            }
            this.loading = true;
            var that = this;
            getKeyInfoDetailList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.getKeyInfoDetail(that.tableData[0].fId, 2);
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        // 详情
        getKeyInfoDetail(id, num) {
            this.loading = true;
            var that = this;
            getKeyInfoDetail({
                fId: id,
                typeid: num
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.formData = res.data.yjQmpMsKeyengineeringinfodetail;
                        that.fileList = res.data.info;//文件
                        that.fileList2 = res.data.video;//视频
                        that.formData.recordfiles = [];
                        if (that.statusNum == 5) {
                            that.formData.monitorCompanyType = that.userInfo.companyNature;
                            that.formData.monitorCompany = that.userInfo.companyName;
                            that.formData.monitorDates = that.timeYMD;
                        }
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        // 刪除文件
        delteFile(file) {
            var that = this;
            for (let index = 0; index < that.fileList.length; index++) {
                if (file.uid == that.fileList[index].uid) {
                    if (that.fileList[index].atta == undefined) {
                        that.formData.recordfiles.push({
                            fId: that.fileList[index].fId,
                        });

                    } else {
                        that.formData.recordfiles.push({
                            fId: that.fileList[index].atta.fId,
                        });
                    }
                    that.fileList.splice(index, 1);
                }
            }


        },

        // 保存
        yj_saveForm() {
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称

            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        saveForm(obj) {
            this.isLock = true;
            saveKeyDetail(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("保存成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log(error.msg);
                });
        },
        // 保存

        // 时间
        getData() {
            var time = new Date();
            const y = time.getFullYear();
            const m = (time.getMonth() + 1 + "").padStart(2, "0");
            const d = (time.getDate() + "").padStart(2, "0");
            const hh = (time.getHours() + "").padStart(2, "0");
            const mm = (time.getMinutes() + "").padStart(2, "0");
            const ss = (time.getSeconds() + "").padStart(2, "0");
            this.timeYMD = `${y}-${m}-${d}`;
            this.timeYmdHms = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        },
    },
};